package com.yjc.hire.server.mq;


import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.yjc.hire.mq.DelayConfig_Industry;
import com.yjc.hire.mq.RabbitMqSMSConfig;
import com.yjc.hire.server.Properties.BaseIndustryProperties;
import com.yjc.hire.server.VO.TopIndustryWithThirdListVO;
import com.yjc.hire.server.service.IndustryService;
import com.yjc.hire.utils.JsonUtils;
import com.yjc.mq.SMSContentQO;
import com.yjc.pojo.Industry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.json.Json;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static com.yjc.hire.base.BaseInfoProperties.MOBILE_SMSCODE;

@Slf4j
@Component
public class RabbitMQDelayConsumer_Industry extends BaseIndustryProperties {
    @Autowired
    private IndustryService industryService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


//    @RabbitListener(queues = {RabbitMqSMSConfig.SMS_QUEUE})
//    public void watchQueue(String payload, Message message){
//        log.info("payload:{}",payload);
//        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
//        log.info("routingKey:{}",routingKey);
//        String mag = payload;
//        log.info("mag:{}",mag);
//        if (routingKey.equalsIgnoreCase(RabbitMqSMSConfig.ROUTING_KEY_SNS_SEND_LOGIN)){
//            SMSContentQO qo = JsonUtils.jsonToObject(mag, SMSContentQO.class);
//            String code = qo.getContent();
//            String mobile = qo.getMobile();
//            redisTemplate.opsForValue().set(MOBILE_SMSCODE + ":" + mobile, code,30 * 60, TimeUnit.SECONDS);
//        }
//    }

    @RabbitListener(queues = {DelayConfig_Industry.QUEUE_DELAY_REFRESH})
    public void watchQueue(Channel channel, Message message) throws IOException {

        String routingKey = message.getMessageProperties().getReceivedRoutingKey();

        String msg = new String(message.getBody());
        if (routingKey.equalsIgnoreCase(DelayConfig_Industry.DELAY_REFRESH_INDUSTRY)){
            stringRedisTemplate.delete(TOP_INDUSTRY_LIST);
            List<Industry> topIndustryList = industryService.getIndusryTopNodeList();
            stringRedisTemplate.opsForValue().set(TOP_INDUSTRY_LIST, JSON.toJSONString(topIndustryList));
            String thirdKeyMulti = THIRD_INDUSTRY_LIST + BY_TOP_ID;
            Set<String> keys = stringRedisTemplate.keys(thirdKeyMulti + "*");
            stringRedisTemplate.delete(keys);
            List<TopIndustryWithThirdListVO> thirdIndustryList = industryService.getAllThirdIndustryList();
            for (TopIndustryWithThirdListVO topIndustryWithThirdListVO : thirdIndustryList) {
                String topId = topIndustryWithThirdListVO.getTopId();
                String thirdKey = THIRD_INDUSTRY_LIST + BY_TOP_ID + topId;
                stringRedisTemplate.opsForValue().set(thirdKey, JSON.toJSONString(topIndustryWithThirdListVO.getThirdIndustryList()));
            }
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    }
}
